<!DOCTYPE html><html lang="cn" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Ansible | 碧蓝幻想</title><meta name="author" content="阿尔托莉雅"><meta name="copyright" content="阿尔托莉雅"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1.playbook的概念： 临时命令可以作为一次性对一组主机运行简单的任务。不过，若要真正发挥Ansible的力量，需要使用playbook  playbook：  playbook是ansible用于配置，部署，和管理被节点的剧本 playbook是一个文本文件，playbook可以轻松重复的方式对一组主机执行多项复杂的任务。 playbook包含有一个或者多个按特定顺序运行的play组成的列">
<meta property="og:type" content="article">
<meta property="og:title" content="Ansible">
<meta property="og:url" content="http://example.com/2023/03/13/3.linux/D2.Ansible%E5%89%A7%E6%9C%AC/index.html">
<meta property="og:site_name" content="碧蓝幻想">
<meta property="og:description" content="1.playbook的概念： 临时命令可以作为一次性对一组主机运行简单的任务。不过，若要真正发挥Ansible的力量，需要使用playbook  playbook：  playbook是ansible用于配置，部署，和管理被节点的剧本 playbook是一个文本文件，playbook可以轻松重复的方式对一组主机执行多项复杂的任务。 playbook包含有一个或者多个按特定顺序运行的play组成的列">
<meta property="og:locale">
<meta property="og:image" content="http://example.com/images/Otherwallpaper/avatar.png">
<meta property="article:published_time" content="2023-03-13T12:32:23.866Z">
<meta property="article:modified_time" content="2023-03-13T12:39:42.643Z">
<meta property="article:author" content="阿尔托莉雅">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/images/Otherwallpaper/avatar.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2023/03/13/3.linux/D2.Ansible%E5%89%A7%E6%9C%AC/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Ansible',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-13 20:39:42'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/images/Otherwallpaper/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">11</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">2</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archive/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/images/"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/images/PCwallpaper/backgroud2.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="碧蓝幻想"><span class="site-name">碧蓝幻想</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archive/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/images/"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Ansible</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-03-13T12:32:23.866Z" title="Created 2023-03-13 20:32:23">2023-03-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-03-13T12:39:42.643Z" title="Updated 2023-03-13 20:39:42">2023-03-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Linux/">Linux</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Ansible"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-playbook的概念："><a href="#1-playbook的概念：" class="headerlink" title="1.playbook的概念："></a>1.playbook的概念：</h2><ul>
<li><p>临时命令可以作为一次性对一组主机运行简单的任务。不过，若要真正发挥Ansible的力量，需要使用playbook</p>
</li>
<li><p>playbook：</p>
<ul>
<li>playbook是ansible用于配置，部署，和管理被节点的剧本</li>
<li>playbook是一个文本文件，playbook可以轻松重复的方式对一组主机执行多项复杂的任务。</li>
<li>playbook包含有一个或者多个按特定顺序运行的play组成的列表</li>
<li>play是针对对清单中选定的主机运行一组有序任务</li>
</ul>
</li>
<li><p>yaml 标记语言</p>
<ul>
<li><p>yaml 标记语言是一个可读性高的用来表达资料序列的格式</p>
</li>
<li><p>YAML 语言特性</p>
<ul>
<li>YAML的可读性好</li>
<li>YAML和脚本语言的交互性好</li>
<li>YAML使用实现语言的数据类型</li>
<li>YAML有一个一致的信息模型</li>
<li>YAML易于实现</li>
<li>YAML可以基于流来处理</li>
<li>YAML表达能力强，扩展性好</li>
</ul>
</li>
<li><p>YAML的三种数据结构</p>
<table>
<thead>
<tr>
<th>数据结构</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>对象</td>
<td>键值对的集合，又称为映射、哈希、字典</td>
</tr>
<tr>
<td>数组</td>
<td>一组按次序排列的值，又称为序列&#x2F;列表</td>
</tr>
<tr>
<td>纯量</td>
<td>单个的、不可再分的值</td>
</tr>
</tbody></table>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 对象：</span></span><br><span class="line"><span class="attr">key:</span></span><br><span class="line">  <span class="attr">key1:</span> <span class="string">vaule1</span></span><br><span class="line">  <span class="attr">key2:</span> <span class="string">vaule2</span></span><br><span class="line"><span class="attr">key:</span> &#123;<span class="attr">key1:</span> <span class="string">value1</span>,<span class="attr">key2:</span> <span class="string">value2</span>&#125;</span><br><span class="line"><span class="comment"># 数组：</span></span><br><span class="line"><span class="attr">key:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">value1</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">value2</span></span><br><span class="line"><span class="string">key:[value1,value2]</span></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h2 id="2-playbook的使用场景："><a href="#2-playbook的使用场景：" class="headerlink" title="2.playbook的使用场景："></a>2.playbook的使用场景：</h2><ul>
<li>执行一些简单的任务，使用ad-hoc命令可以方便的解决问题</li>
<li>需要大量的操作的时候，执行的ad-hoc命令是不合适的，这时候最好使用playbook</li>
</ul>
<h2 id="3-playbook的属性："><a href="#3-playbook的属性：" class="headerlink" title="3.playbook的属性："></a>3.playbook的属性：</h2><table>
<thead>
<tr>
<th>属性</th>
<th>必选</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>name</td>
<td>no</td>
<td>剧本的说明信息</td>
</tr>
<tr>
<td>hosts</td>
<td></td>
<td>主机组</td>
</tr>
<tr>
<td>remote_user</td>
<td>no</td>
<td>远程用户</td>
</tr>
<tr>
<td>t asks</td>
<td></td>
<td>任务列表</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>Tasks属性</th>
<th>必选</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>name</td>
<td>no</td>
<td>任务的名字，本任务的说明</td>
</tr>
<tr>
<td>[模块]</td>
<td></td>
<td>表示本任务使用的模块</td>
</tr>
</tbody></table>
<h2 id="4-playbook的格式："><a href="#4-playbook的格式：" class="headerlink" title="4.playbook的格式："></a>4.playbook的格式：</h2><ul>
<li><p>YAML的格式如下：</p>
<ul>
<li><p>文件的第一行应该以“—”（三个连字符）开始，表明YAML文件的开始。</p>
</li>
<li><p>#之后的内容表示注释</p>
</li>
<li><p>YAML中的列表元素以“-”开头并且跟着一个空格</p>
</li>
</ul>
</li>
<li><p>修改&#x2F;etc&#x2F;.vimrc文件，使用所有用户的yaml文件缩进变为2字符</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">autocmd FileType yaml setlocal ai ts=2 sw=2 et</span><br></pre></td></tr></table></figure>
</li>
<li><p>示例一：创建uid为3333，shell为不可登录，名字为user1的剧本</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim user.yml</span><br></pre></td></tr></table></figure>

<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">user</span></span><br><span class="line">  <span class="attr">remote_user:</span> <span class="string">root</span></span><br><span class="line">  <span class="attr">hosts:</span> <span class="string">webservers</span></span><br><span class="line">  <span class="attr">tasks:</span> </span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">user1</span></span><br><span class="line">    <span class="attr">user:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">user1</span></span><br><span class="line">      <span class="attr">uid:</span> <span class="number">3333</span></span><br><span class="line">      <span class="attr">shell:</span> <span class="string">/sbin/nologin</span> </span><br><span class="line">      <span class="attr">state:</span> <span class="string">present</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>示例二：创建redhat.repo的yum源，并安装redis数据库，开机自启启动服务</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">yum</span> <span class="string">install</span></span><br><span class="line">  <span class="attr">hosts:</span> <span class="string">webservers</span></span><br><span class="line">  <span class="attr">remote_user:</span> <span class="string">root</span></span><br><span class="line">  <span class="attr">tasks:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">wget</span> <span class="string">centos....repo</span></span><br><span class="line">      <span class="attr">get_url:</span></span><br><span class="line">        <span class="attr">url:</span> <span class="string">https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo</span></span><br><span class="line">        <span class="attr">dest:</span> <span class="string">/etc/yum.repos.d/Centos-8.5.repo</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">yum</span> <span class="string">repolist</span></span><br><span class="line">      <span class="attr">shell:</span> <span class="string">yum</span> <span class="string">repolist</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">redis</span></span><br><span class="line">      <span class="attr">yum:</span></span><br><span class="line">        <span class="attr">name:</span> <span class="string">redis</span></span><br><span class="line">        <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">systemctl</span> <span class="string">redis</span></span><br><span class="line">      <span class="attr">service:</span></span><br><span class="line">      	<span class="attr">name:</span> <span class="string">reids</span></span><br><span class="line">      	<span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line">      	<span class="attr">enabled:</span> <span class="literal">yes</span></span><br></pre></td></tr></table></figure>
</li>
<li></li>
</ul>
<h2 id="5-Playbook的运行方式："><a href="#5-Playbook的运行方式：" class="headerlink" title="5.Playbook的运行方式："></a>5.Playbook的运行方式：</h2><ul>
<li><p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ansible-playbook &lt;filename.yml&gt; ... [options]</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数类型：</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>–check  or -C</td>
<td>只检测可能会发生的改变，但不真正执行操作</td>
</tr>
<tr>
<td>–list-hosts</td>
<td>列出运行任务的主机</td>
</tr>
<tr>
<td>–list-tags</td>
<td>列出playbook文件中定义所有的tags</td>
</tr>
<tr>
<td>–list-tasks</td>
<td>列出playbook文件中定义的所以任务集</td>
</tr>
<tr>
<td>–limit</td>
<td>主机列表 只针对主机列表中的某个主机或者某个组执行</td>
</tr>
<tr>
<td>-f</td>
<td>指定并发数，默认为5个</td>
</tr>
<tr>
<td>-t</td>
<td>指定tags运行，运行某一个或者多个tags。（前提playbook中有定义tags）</td>
</tr>
<tr>
<td>-v</td>
<td>显示任务结果</td>
</tr>
<tr>
<td>-vv</td>
<td>显示任务结果和配置</td>
</tr>
<tr>
<td>-vvv</td>
<td>显示任务结果和配置，受管主机连接信息</td>
</tr>
<tr>
<td>-vvvv</td>
<td>更详细</td>
</tr>
</tbody></table>
</li>
</ul>
<h2 id="6-playbook变量"><a href="#6-playbook变量" class="headerlink" title="6.playbook变量"></a>6.playbook变量</h2><ul>
<li>变量名称必须是以字母开头，并且只能含有字母、数字和下划线</li>
<li>变量优先级：（高–低）<ol>
<li><p>全局范围：从命令行或ansible配置设置的变量</p>
</li>
<li><p>play范围：在play和相关结构中设置的变量</p>
</li>
<li><p>主机范围：由清单、事实收集或注册的任务，在主机和个别主机上设置的变量</p>
</li>
</ol>
</li>
</ul>
<h3 id="1-自定义变量的定义和引入："><a href="#1-自定义变量的定义和引入：" class="headerlink" title="1.自定义变量的定义和引入："></a>1.自定义变量的定义和引入：</h3><ul>
<li><p>命令行定义变量：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ansible-playbook -e &#x27;pkname1=httpd pkname2=vsftpd&#x27; app.yml </span><br></pre></td></tr></table></figure>

<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span>  <span class="string">all</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">package</span></span><br><span class="line">    <span class="attr">yum:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123;  pkname1 &#125;&#125;</span>&quot;</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">package</span></span><br><span class="line">    <span class="attr">yum:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; pkname2 &#125;&#125;</span>&quot;</span><span class="string">=</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>playbook中定义变量 vars</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span>  <span class="string">all</span></span><br><span class="line">  <span class="attr">vars:</span>     <span class="comment"># 定义var</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">pkname1:</span> <span class="string">httpd</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">pkname2:</span> <span class="string">vsftpd</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">package</span></span><br><span class="line">    <span class="attr">yum:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123;  pkname1 &#125;&#125;</span>&quot;</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">package</span></span><br><span class="line">    <span class="attr">yum:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; pkname2 &#125;&#125;</span>&quot;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>playbook中导入变量文件vars_files</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">vim</span> <span class="string">var.yml</span></span><br><span class="line"><span class="comment"># 修改内容：</span></span><br><span class="line"><span class="attr">var1:</span> <span class="string">httpd</span> </span><br><span class="line"><span class="attr">var2:</span> <span class="string">vsftpd</span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">- hosts:  all</span><br><span class="line">  vars_files: ./user_var.yml     # 导入文件</span><br><span class="line">  - name: install package</span><br><span class="line">    yum:</span><br><span class="line">      name: &quot;&#123;&#123;  pkname1 &#125;&#125;&quot;</span><br><span class="line">  - name: install package</span><br><span class="line">    yum:</span><br><span class="line">      name: &quot;&#123;&#123; pkname2 &#125;&#125;&quot;</span><br></pre></td></tr></table></figure>
</li>
<li><p>定义主机变量：（主机清单）group_vars&#x2F;文件名，host_vars&#x2F;文件名</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[web]</span><br><span class="line">172.16.20.131  http_port=81</span><br><span class="line">172.16.20.132  http_port=82</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[web:vars]                        ----主机组变量</span><br><span class="line">nodename=www       </span><br><span class="line">domainname=openlab.com</span><br><span class="line">[server1]</span><br><span class="line">demo1.example.com</span><br><span class="line">demo2.example.com</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[servers:children]                -----嵌套主机组变量</span><br><span class="line">Server1</span><br><span class="line">Server2</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用数组作为变量</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">vim</span> <span class="string">user.yml</span></span><br><span class="line"><span class="attr">USERLIST:</span></span><br><span class="line">  <span class="attr">lee:</span></span><br><span class="line">    <span class="string">age:18</span></span><br><span class="line">    <span class="string">obj:linux</span></span><br><span class="line">  <span class="attr">westos:</span></span><br><span class="line">    <span class="string">age:20</span></span><br><span class="line">    <span class="string">obj:java</span></span><br></pre></td></tr></table></figure>

<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 引入方式一</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">test</span></span><br><span class="line">  <span class="string">hosts:test</span></span><br><span class="line">  <span class="string">vars_files:./user.yml</span></span><br><span class="line">  <span class="attr">tasks:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">user</span></span><br><span class="line">      <span class="attr">debug:</span></span><br><span class="line">        <span class="attr">msg:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123;USERLIST.lee.age&#125;&#125;</span>&quot;</span></span><br><span class="line"><span class="comment"># 引入方式二        </span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">test</span></span><br><span class="line">  <span class="string">hosts:test</span></span><br><span class="line">  <span class="string">vars_files:./user.yml</span></span><br><span class="line">  <span class="attr">tasks:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">user</span></span><br><span class="line">    <span class="attr">debug:</span></span><br><span class="line">    <span class="string">var:USERLIST[&#x27;lee&#x27;][&#x27;age&#x27;]</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-系统变量的注册："><a href="#2-系统变量的注册：" class="headerlink" title="2.系统变量的注册："></a>2.系统变量的注册：</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">- name:test</span><br><span class="line">  hosts:172.25.254.100</span><br><span class="line">  tasks:</span><br><span class="line">  - name:shell</span><br><span class="line">    shell:</span><br><span class="line">      date</span><br><span class="line">    register: WESTOS           # 把date这个命令的所有输出全部注册成了WESTOS这个变量</span><br><span class="line">  - debug:</span><br><span class="line">      var: WESTOS.rc           # 变量引用</span><br></pre></td></tr></table></figure>

<h3 id="3-事实变量"><a href="#3-事实变量" class="headerlink" title="3.事实变量"></a>3.事实变量</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- name: test</span><br><span class="line">  hosts: 172.25.254.100</span><br><span class="line">  tasks:</span><br><span class="line">  - debug:</span><br><span class="line">      var: ansible_facts[&#x27;ens3&#x27;][&#x27;ipv4&#x27;][&#x27;address&#x27;]    #取地址</span><br><span class="line">      var: ansible_facts[&#x27;fqdn&#x27;]                       #取主机名称</span><br></pre></td></tr></table></figure>



<h2 id="7-ansible-vault加密文件："><a href="#7-ansible-vault加密文件：" class="headerlink" title="7.ansible vault加密文件："></a>7.ansible vault加密文件：</h2><h3 id="1-ansible-vault概念："><a href="#1-ansible-vault概念：" class="headerlink" title="1.ansible vault概念："></a>1.ansible vault概念：</h3><ul>
<li>ansible可能需要访问密码或者API秘钥等敏感数据</li>
<li>通常，此信息可能以纯文本形式存储在清单变量或其他ansible文件中</li>
<li>任何有权访问ansible文件的用户或存储这些ansible文件的版本控制系统都能访问如此敏感数据。这显然存在安全风险。</li>
<li>作用： 可以加密和解密任何由ansible使用的结构化数据文件</li>
<li>范围：Ansible vault可以加密任何由ansible使用的结构化数据文件。<ul>
<li>清单变量</li>
<li>playbook中含有的变量文件</li>
<li>在执行playbook时作为参数传递的变量文件</li>
<li>ansible角色中定义的变量</li>
</ul>
</li>
<li>ansible-vault并不实施自有的加密函数，而是使用外部python工具</li>
</ul>
<h3 id="2-ansible-vault增删改查"><a href="#2-ansible-vault增删改查" class="headerlink" title="2.ansible vault增删改查"></a>2.ansible vault增删改查</h3><ul>
<li><p>创建加密文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@quruixiang ~]# ansible-vault create secrt.yml</span><br><span class="line">New Vault password: </span><br><span class="line">Confirm New Vault password: </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">编辑内容：</span></span><br><span class="line">username: root</span><br><span class="line">password: 123456</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看加密文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">方式一：</span></span><br><span class="line">[root@quruixiang ~]# ansible-vault view secrt.yml </span><br><span class="line">Vault password: </span><br><span class="line">username: root</span><br><span class="line">password: 110119</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">方式二：文件指定（必须修改文件权限为600）</span></span><br><span class="line">echo 110119 &gt; user_passwd.txt</span><br><span class="line">chmod 600 user_passwd.txt </span><br><span class="line">ansible-vault view create_user.yml --vault-password-file=user_passwd.txt</span><br></pre></td></tr></table></figure>
</li>
<li><p>编辑加密文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@quruixiang /]# ansible-vault edit secrt.yml </span><br><span class="line">Vault password: </span><br></pre></td></tr></table></figure>
</li>
<li><p>重置密码：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@quruixiang /]# ansible-vault rekey secrt.yml </span><br><span class="line">Vault password: </span><br><span class="line">New Vault password: </span><br><span class="line">Confirm New Vault password: </span><br><span class="line">Rekey successful</span><br></pre></td></tr></table></figure>
</li>
<li><p>加密已有文件：（加上密码）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ansible-vault encryt secrt.yml</span><br></pre></td></tr></table></figure>
</li>
<li><p>对加密文件进行解密：(删除密码)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ansible-vault decrypt secrt.yml</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-在引用和执行："><a href="#3-在引用和执行：" class="headerlink" title="3.在引用和执行："></a>3.在引用和执行：</h3><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">user</span></span><br><span class="line">  <span class="attr">remote_user:</span> <span class="string">root</span></span><br><span class="line">  <span class="attr">hosts:</span> <span class="string">webservers</span></span><br><span class="line">  <span class="attr">vars_files:</span></span><br><span class="line">  	<span class="bullet">-</span> <span class="string">./secrt.yml</span>            <span class="comment"># 引入加密文件</span></span><br><span class="line">  <span class="attr">tasks:</span> </span><br><span class="line">  <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">user1</span></span><br><span class="line">    <span class="attr">user:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; username1 &#125;&#125;</span>&quot;</span></span><br><span class="line">      <span class="attr">password:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; password1 &#125;&#125;</span>&quot;</span>     <span class="comment"># 传递的用户密码需要使用python进行加密，否者失败</span></span><br><span class="line">      <span class="attr">shell:</span> <span class="string">/sbin/nologin</span> </span><br><span class="line">      <span class="attr">state:</span> <span class="string">present</span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">方式一：咨询</span></span><br><span class="line">ansible-playbook /etc/ansible/playbook/user1.yml --ask-vault-pass</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">方式二：指定密码文件,</span></span><br><span class="line">ansible-playbook /etc/ansible/playbook/user1.yml --ask-vault-file=passwd</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">方式三：直接在ansible配置文件中配置vault_password_file</span></span><br><span class="line">vim /etc/ansible/ansible.cfg</span><br><span class="line"></span><br><span class="line">vault_password_file=./secrt/passwd</span><br><span class="line"></span><br><span class="line">ansible-playbook /etc/ansible/playbook/user1.yml</span><br></pre></td></tr></table></figure>



<h2 id="8-jinjia2模板："><a href="#8-jinjia2模板：" class="headerlink" title="8.jinjia2模板："></a>8.jinjia2模板：</h2></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="http://example.com">阿尔托莉雅</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://example.com/2023/03/13/3.linux/D2.Ansible%E5%89%A7%E6%9C%AC/">http://example.com/2023/03/13/3.linux/D2.Ansible%E5%89%A7%E6%9C%AC/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Linux/">Linux</a></div><div class="post_share"><div class="social-share" data-image="/images/Otherwallpaper/avatar.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/A1.MySQL-%E6%AC%A7%E9%B9%8F%E7%AF%87/" title="SQL篇-MySQL数据库"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">SQL篇-MySQL数据库</div></div></a></div><div class="next-post pull-right"><a href="/2023/03/13/3.linux/D1.Ansible%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7/" title="Ansible基础"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">Ansible基础</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2023/03/13/3.linux/A1.linux%E5%9F%BA%E7%A1%80/" title="Linux基础"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Linux基础</div></div></a></div><div><a href="/2023/03/13/3.linux/A2.Linux%E5%88%9D%E7%BA%A7%E5%91%BD%E4%BB%A4/" title="Linux初级命令"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Linux初级命令</div></div></a></div><div><a href="/2023/03/13/3.linux/A3.Linux%E4%B8%AD%E7%BA%A7%E5%91%BD%E4%BB%A4/" title="Linux中级命令"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Linux中级命令</div></div></a></div><div><a href="/2023/03/13/3.linux/A4.Shell%E8%84%9A%E6%9C%AC/" title="Shell脚本"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Shell脚本</div></div></a></div><div><a href="/2023/03/13/3.linux/B1.Docker%E5%AE%B9%E5%99%A8/" title="Docker容器"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Docker容器</div></div></a></div><div><a href="/2023/03/13/3.linux/C1.git%E5%B7%A5%E5%85%B7/" title="git工具"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">git工具</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/images/Otherwallpaper/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">阿尔托莉雅</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">11</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">2</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://toscode.gitee.com/blue-fantasy" target="_blank" title="Gitee"><i class="fab fa-github"></i></a><a class="social-icon" href="/1095322098@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-playbook%E7%9A%84%E6%A6%82%E5%BF%B5%EF%BC%9A"><span class="toc-number">1.</span> <span class="toc-text">1.playbook的概念：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-playbook%E7%9A%84%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%EF%BC%9A"><span class="toc-number">2.</span> <span class="toc-text">2.playbook的使用场景：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-playbook%E7%9A%84%E5%B1%9E%E6%80%A7%EF%BC%9A"><span class="toc-number">3.</span> <span class="toc-text">3.playbook的属性：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-playbook%E7%9A%84%E6%A0%BC%E5%BC%8F%EF%BC%9A"><span class="toc-number">4.</span> <span class="toc-text">4.playbook的格式：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-Playbook%E7%9A%84%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F%EF%BC%9A"><span class="toc-number">5.</span> <span class="toc-text">5.Playbook的运行方式：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-playbook%E5%8F%98%E9%87%8F"><span class="toc-number">6.</span> <span class="toc-text">6.playbook变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E5%BC%95%E5%85%A5%EF%BC%9A"><span class="toc-number">6.1.</span> <span class="toc-text">1.自定义变量的定义和引入：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E7%B3%BB%E7%BB%9F%E5%8F%98%E9%87%8F%E7%9A%84%E6%B3%A8%E5%86%8C%EF%BC%9A"><span class="toc-number">6.2.</span> <span class="toc-text">2.系统变量的注册：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BA%8B%E5%AE%9E%E5%8F%98%E9%87%8F"><span class="toc-number">6.3.</span> <span class="toc-text">3.事实变量</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-ansible-vault%E5%8A%A0%E5%AF%86%E6%96%87%E4%BB%B6%EF%BC%9A"><span class="toc-number">7.</span> <span class="toc-text">7.ansible vault加密文件：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-ansible-vault%E6%A6%82%E5%BF%B5%EF%BC%9A"><span class="toc-number">7.1.</span> <span class="toc-text">1.ansible vault概念：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-ansible-vault%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"><span class="toc-number">7.2.</span> <span class="toc-text">2.ansible vault增删改查</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%9C%A8%E5%BC%95%E7%94%A8%E5%92%8C%E6%89%A7%E8%A1%8C%EF%BC%9A"><span class="toc-number">7.3.</span> <span class="toc-text">3.在引用和执行：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-jinjia2%E6%A8%A1%E6%9D%BF%EF%BC%9A"><span class="toc-number">8.</span> <span class="toc-text">8.jinjia2模板：</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/B3.NoSQL%E6%95%B0%E6%8D%AE%E5%BA%93%20-%20MongDB/" title="NoSQL篇-MongDB数据库">NoSQL篇-MongDB数据库</a><time datetime="2023-03-13T12:32:38.445Z" title="Created 2023-03-13 20:32:38">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/B2.NoSQL%E6%95%B0%E6%8D%AE%E5%BA%93%20-%20Redis/" title="NoSQL篇-Redis数据库">NoSQL篇-Redis数据库</a><time datetime="2023-03-13T12:32:38.441Z" title="Created 2023-03-13 20:32:38">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/A1.MySQL-%E6%AC%A7%E9%B9%8F%E7%AF%87/" title="SQL篇-MySQL数据库">SQL篇-MySQL数据库</a><time datetime="2023-03-13T12:32:38.437Z" title="Created 2023-03-13 20:32:38">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/3.linux/D2.Ansible%E5%89%A7%E6%9C%AC/" title="Ansible">Ansible</a><time datetime="2023-03-13T12:32:23.866Z" title="Created 2023-03-13 20:32:23">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/3.linux/D1.Ansible%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7/" title="Ansible基础">Ansible基础</a><time datetime="2023-03-13T12:32:23.861Z" title="Created 2023-03-13 20:32:23">2023-03-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 阿尔托莉雅</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Toggle Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>